package com.naa17d.alert4gps;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity {
	EditText text; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
		text = (EditText)findViewById(R.id.text_displaySpeed);  
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
        showLocation(location);  
          
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){  
  
            public void onLocationChanged(Location location) {  
                // TODO Auto-generated method stub  
                showLocation(location);  
            }  
  
            public void onProviderDisabled(String provider) {  
                // TODO Auto-generated method stub  
                showLocation(null);  
            }  
  
            public void onProviderEnabled(String provider) {  
                // TODO Auto-generated method stub  
                showLocation(locationManager.getLastKnownLocation(provider));  
            }  
  
            public void onStatusChanged(String provider, int status, Bundle extras) {  
                // TODO Auto-generated method stub  
            }  
              
        });  
	}
	
	public void showLocation(Location currentLocation){  
        if(currentLocation != null){  
            String s = "";  
            s += " Current Location: (";  
            s += currentLocation.getLongitude();  
            s += ",";  
            s += currentLocation.getLatitude();  
            s += ")\n Speed: ";  
            s += currentLocation.getSpeed();  
            s += "\n Direction: ";  
            s += currentLocation.getBearing();  
            text.setText(s);  
        }  
        else{  
            text.setText("");  
        }  
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
